﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;

using ConfigDashboard.Models;


namespace ConfigDashboard.Controllers
{
    public class AdminController : Controller
    {
        private ConfigSettingsEntities db = new ConfigSettingsEntities();

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }


        //
        // GET: /Admin/Import

        public ActionResult Import()
        {
            return View();
        }


        //
        // POST: /Admin/Upload

        [HttpPost]
        public ActionResult Upload()
        {
            SpreadsheetImportResult result = new SpreadsheetImportResult();

            if (null != Request.Files && Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];

                var fileName = Path.GetFileName(file.FileName);
                if (!string.IsNullOrEmpty(fileName))
                {
                    SettingsImporter importer = new SettingsImporter();
                    importer.ImportCsvFile(result, file.InputStream);
                }
            }

            return View("Import", result);
        }


        /// <summary>
        /// Releases unmanaged resources and optionally releases managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
   }
}